//
//  TLVisitorView.swift
//  TLSinaWeiBo
//
//  Created by 谭亮 on 16/5/11.
//  Copyright © 2016年 tanliang. All rights reserved.
//

import UIKit
//1->定义协议       <NSObject>
@objc protocol TLVisitorViewDelegate: NSObjectProtocol {
    //2.定义协议方法
    //->注册按钮被点击后的代理方法 必须实现的方法.在协议跟方法前面都加上 optional 成为可选实现方法
    optional func visitorViewDidClickRegister()
    //->登录按钮被点击后的代理方法
    func visitorViewDidClickLogin()
}

//:自定义的访客视图
class TLVisitorView: UIView {

    var name: String
    
    //3定义代理属性
    //->OC中写: @property() id<TLVisitorViewDelegate> delegate
    weak var delegate: TLVisitorViewDelegate?
    
    // MARK: - 公开的方法
    //:设置访客视图内容
    func setupVisitorInfo(imageName: String, message: String) {
        //:1.将转轮图片隐藏
        iconView.image = UIImage(named: imageName)
        //:2.设置messageLabel
        messageLabel.text = message
        //:3.隐藏小房子
        homeView.hidden = true
        
        //:4.隐藏遮盖
        coverView.hidden = true
    }
    
    //:->转轮旋转动画
    func startRotationAnimation() {
        //:创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //:
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //:->核心动画完成的时候不要移除
        anim.removedOnCompletion = false
        
        //:将核心动画添加到图层上
        iconView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    //:这个构造函数通过xib/sb里面创建才会走这个方法
    //:UIKit的里面的view即可以通过代码来创建.也可以通过xib/sb创建 所有要写上这个方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //:override:是构造函数 看父类有没有和这个一样的构造函数
    override init(frame: CGRect) {
        //:在super.init 之前给存储性属性赋值
        //:调用对象方法不能放到super.init之前 在super.init之前,这个对象还没初始化好(创建),对象没创建好是不能调用对象的方法的
        self.name = ""
        super.init(frame: frame)
        
        prepareUI()
    }
    
    //: 设置UI
    private func prepareUI() {
        
        //:设置背景颜色
        //self.backgroundColor = UIColor(red: 232 / 255.0, green: 232 / 255.0, blue: 232 / 255.0, alpha: 1)
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        //:将子控件添加进来
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(messageLabel)
        self.addSubview(regusterButton)
        self.addSubview(loginButton)
        //:去掉默认约束
        self.iconView.translatesAutoresizingMaskIntoConstraints = false
        self.coverView.translatesAutoresizingMaskIntoConstraints = false
        self.homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        regusterButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        //:添加约束(最好只添加一次,多次会冲突)
        //:创建的约束都要往要约束的view的父控件上面添加
        
        //:转轮 CenterX和父控件的CenterX重合
        //item:要添加的约束的view
        //attribute:是要添加的View的属性
        //toItem:表示要参照的view
        //attribute:要参照的View的属性
        //:将约束添加到view上面去
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //:CenterY和父控件的CenterY重合
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //:遮盖约束
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: regusterButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        //:小房子
        //:CenterX转轮的CenterX
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
    
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //:约束消息lable
        //宽度
        //:当参照的view为nil的时候,属性为NSLayoutAttribute.NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        //:CenterX和父控件CenterX
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //:Top和转轮底部
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        //:约束注册按钮
        //顶部和lable底部
        self.addConstraint(NSLayoutConstraint(item: regusterButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //:左边和label对齐
        self.addConstraint(NSLayoutConstraint(item: regusterButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //:宽度
        self.addConstraint(NSLayoutConstraint(item: regusterButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //:高度
        self.addConstraint(NSLayoutConstraint(item: regusterButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //:登录按钮
        //:顶部和label对齐
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))

        //:右边和label对齐
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //:宽度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //:高度
        self.addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
    }
    
    // MARK: - 按钮的点击事件
    //:->按钮的点击事件是系统调用的,当把按钮点击事件private后,系统就找不到这个方法就会报错误
    //:但在private前加 @objc就不会有错误。@objc->让系统可以找到我们的方法
    //:@objc private func didClickRegister()
    @objc private func didClickRegister() {
        //print("__FUNCTION__")
        //:view 模态madal不出控制器来,所有需要将事件传递给控制器
        //->4.调用代理的方法
        //:->在后面加上问好说明此方法如果有就调用,没有就不调用
        delegate?.visitorViewDidClickRegister?()
    }
    
    @objc private func didClickLogin() {
        //print("__FUNCTION__")
        //->4.调用代理的方法
        delegate?.visitorViewDidClickLogin()
    }
    
    // MARK: - 懒加载
    //:-> 所有的控件
    //:1.转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //:遮盖视图:UIView有个maskView属性,不能起这个名字
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //:2.小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //:3.消息文字
    private lazy var messageLabel: UILabel = {
       
        let labels = UILabel()
        //:设置label
        labels.text = "关注一些人,看看有什么惊喜!"
        labels.textColor = UIColor.blackColor()
        labels.font = UIFont.systemFontOfSize(16)
        labels.textAlignment = NSTextAlignment.Center
        labels.numberOfLines = 0
        labels.sizeToFit()
    
        return labels
    }()
    
    //:注册按钮
    private lazy var regusterButton: UIButton = {
       
        let buttons = UIButton()
        //:设置
        buttons.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        buttons.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        buttons.setTitle("注册", forState: UIControlState.Normal)
        //buttons.titleLabel?.texr 不要这样设置,到时候出不来
        
        buttons.addTarget(self, action: "didClickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        return buttons
    }()
    
    //:登录按钮
    private lazy var loginButton: UIButton = {
       let buttons = UIButton()
        //:设置
        buttons.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        buttons.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        buttons.setTitle("登录", forState: UIControlState.Normal)
        
        buttons.addTarget(self, action: "didClickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return buttons
    }()
}
